<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构内容 头部-->
      <el-card class="tree-card">
        <!-- 放置结构内容 -->
        <tree-tools :treeNode="company" :isRoot="true" @addDepts="addDepts" />
        <!-- 放置一个el-tree -->
        <el-tree
          :data="departs"
          :props="defaultProp"
          :default-expand-all="true"
        >
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次-->
          <!-- 作用域插槽 slot-scoped="obj" 接收传递给插槽的数据 data每个节点的数据对象-->
          <tree-tools
            @delDepts="getDepartments"
            slot-scope="{ data }"
            :treeNode="data"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 放置弹层 -->
    <add-dept
      @addDepts="getDepartments"
      :showDialog.sync="showDialog"
      :tree-node="node"
      @changeDialog="test"
      ref="addDept"
    />
  </div>
</template>

<script>
import TreeTools from "./components/tree-tools.vue";
import { getDepartments, getDepartDetail } from "@/api/departments";
import { tranListToTreeData } from "@/utils";
import AddDept from "./components/add-dept.vue";
export default {
  components: {
    TreeTools,
    AddDept,
  },
  data() {
    return {
      company: { name: "", manager: "" },
      departs: [],
      defaultProp: {
        label: "name", //表示从这个属性显示内容
        children: "children", //从这个属性去找子节点
      },
      showDialog: false, // 默认不显示弹层
      node: null, //记录当前点击的node节点
    };
  },
  created() {
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments();
      this.company = { name: result.companyName, manager: "负责人", id: "" };
      this.departs = tranListToTreeData(result.depts, ""); //需要将其转化成树形结构
    },

    // 监听tree-tools中触发的点击添加子部门的事件
    // node 是我们当前点击的部门
    addDepts(node) {
      // 新增
      this.showDialog = true; //显示弹层
      this.node = node;
    },
    test(value) {
      this.showDialog = value;
    },
    editDepts(node) {
      // 编辑
      this.showDialog = true; // 弹出层
      this.node = node;
      // 这里调用获取部门详情的方法
      this.$refs.addDept.getDepartDetail(node.id);
    },
  },
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>